<?php
namespace App\Admin\Controllers\Reimburse;

use App\Importer\Reimburse\ApplyImporter;
use App\Util\SerialNo;
use App\Models\Basics\SysCountry;
use App\Models\Basics\Currency;
use App\Models\Basics\ExchangeRate;
use App\Models\Basics\SysICountry;
use App\Models\Product\ProductAmazonSku;
use App\Models\Reimburse\ReimburseApply;
use App\Util\RExcel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Illuminate\Support\Facades\DB;
class ApplyUploadController extends AdminController{

    protected $title = '返款申请上传';

    protected function grid()
    {
        $type = request('type');
        return view("reimburse.apply-upload.index",compact('type'));
    }

    public function upload(Request $request){
        $file = $request->file('source_file');
        if ($file->isValid()) {
            $action = $request->get('action');
            $param = $request->get('param');
            if($param){
                $re = $this->{'_import'.$action}($file,$param);
            }else{
                $re = $this->{'_import'.$action}($file);
            }
            if(is_array($re)) {
                if($re['flag']){
                    admin_toastr('文件：'.$re['data'].' 上传成功！');
                }else{
                    admin_toastr($re['data'],'error');
                }
            }else{
                return $re;
            }
        }else{
            admin_toastr('无效文件！','error');
        }
        return back()->withInput();
    }

    private function _importApply(UploadedFile $file){
        try{
            $data = Excel::toArray(new ApplyImporter(),$file);
            $data = $data[0];
            if(!$data)return ['flag' => false,'data' => '请上传包含有数据的文件！'];
            $errors = [];
            $count = 1;
            $userId = Admin::user()->id;
            $orderNoArr = [];
            $items = [];
            $date = date('Y-m-d H:i:s');
            foreach($data as $row){
                $count++;
                $_country = $row[0] ? trim($row[0]) : '';
                $paidType = $row[1] ? trim($row[1]) : '';
                $feeType = $row[2] ? trim($row[2]) : '';
                $rType = $row[3] ? trim($row[3]) : '';
                $orderNo = $row[4] ? trim($row[4]) : '';
                $orderAtten = $row[5] ? trim($row[5]) : '';
                $sellerSku = $row[6] ? trim($row[6]) : '';
                $paidMethod = $row[7] ? mergeSpaces(trim($row[7])) : '';
                $paidAccountNo = $row[8] ? trim($row[8]) : '';
                $_currency = $row[9] ? trim($row[9]) : '';
                $paidAmount1 = $row[10] ? trim($row[10]) : '';
                $paidAmount2 = $row[11] ? trim($row[11]) : '';
                $paidAmount3 = $row[12] ? trim($row[12]) : '';
                $paidAmount4 = $row[13] ? trim($row[13]) : '';
                $paidMethod4 = $row[14] ? mergeSpaces(trim($row[14])) : '';
                $paidAccountNo4 = $row[15] ? trim($row[15]) : '';
                $_currency4 = $row[16] ? trim($row[16]) : '';
                $asin = $row[17] ? trim($row[17]) : '';
                $remarks = $row[18] ? trim($row[18]) : '';
                if(!$_country || !$paidType || !$feeType || !$paidMethod || !$paidAccountNo || !$_currency){
                    $errors[$count] = '必填项为空';
                    continue;
                }
                $country = strtoupper($_country);
                $countryId = SysCountry::getIdByCountry($country);
                if(!$countryId){
                    $errors[$count] = $_country.'系统不存在';
                    continue;
                }
                $areaId = SysICountry::getAreaIdByCountryId($countryId);
                if(!$areaId){
                    $errors[$count] = $_country.'系统未匹配到站点';
                    continue;
                }
                if(!in_array($paidType,ReimburseApply::$_paidType)){
                    $errors[$count] = $paidType.'返款类型不存在，请选择'.implode('、',ReimburseApply::$_paidType);
                    continue;
                }
                $paidTypeId = array_search($paidType,ReimburseApply::$_paidType);
                if(!in_array($feeType,ReimburseApply::$_feeType)){
                    $errors[$count] = $feeType.'费用类型不存在，请选择'.implode('、',ReimburseApply::$_feeType);
                    continue;
                }
                $feeTypeId = array_search($feeType,ReimburseApply::$_feeType);
                if($feeTypeId == 1 || $feeTypeId == 2){
                    if($feeTypeId == 1){
                        if(!$rType){
                            $errors[$count] = '刷单类型不能为空！';
                            continue;
                        }
                        if(!in_array($rType,ReimburseApply::$_rType)){
                            $errors[$count] = $_currency.'刷单类型不存在，请选择'.implode('、',ReimburseApply::$_rType);
                            continue;
                        }
                    }
                    if(!$sellerSku){
                        $errors[$count] = '亚马逊SKU不能为空！';
                        continue;
                    }
                    $pas = ProductAmazonSku::query()
                        ->where('seller_sku',$sellerSku)
                        ->where('country_id',$countryId)
                        ->first();
                    if(!$pas){
                        $errors[$count] = '系统不存在该国家的亚马逊SKU：'.$sellerSku;
                        continue;
                    }
                }elseif($feeTypeId == 4){
                    if(!$asin){
                        $errors[$count] = 'ASIN不能为空！';
                        continue;
                    }
                    $pas = ProductAmazonSku::query()
                        ->where('country_id',$countryId)
                        ->where('asin',$asin)
                        ->first();
                    if(!$pas){
                        $errors[$count] = '系统未匹配到该ASIN：'.$asin;
                        continue;
                    }
                }else{
                    if($sellerSku){
                        $pas = ProductAmazonSku::query()
                            ->where('seller_sku',$sellerSku)
                            ->where('country_id',$countryId)
                            ->first();
                        if(!$pas){
                            $errors[$count] = '系统不存在该国家的亚马逊SKU：'.$sellerSku;
                            continue;
                        }
                    }
                }
                if(!in_array($paidMethod,ReimburseApply::$_paidMethod)){
                    $errors[$count] = $paidMethod.'支付方式不存在，请选择'.implode('、',ReimburseApply::$_paidMethod);
                    continue;
                }
                $paidMethodId = array_search($paidMethod,ReimburseApply::$_paidMethod);
                $currency = strtoupper($_currency);
                if(!Currency::existByCode($currency)){
                    $errors[$count] = $_currency.'币种不存在';
                    continue;
                }
                $currency4 = null;
                if($paidTypeId == 1 || $paidTypeId == 3){
                    if(!is_numeric($paidAmount1)){
                        $errors[$count] = '一次性付款金额格式出错';
                        continue;
                    }
                    if($paidTypeId == 3){
                        if(!is_numeric($paidAmount4)){
                            $errors[$count] = '佣金金额格式出错';
                            continue;
                        }
                        if(!$_currency4){
                            $errors[$count] = '佣金币种不能为空';
                            continue;
                        }
                        $currency4 = strtoupper($_currency4);
                        if(!Currency::existByCode($currency4)){
                            $errors[$count] = $_currency4.'佣金币种不存在';
                            continue;
                        }
                        if($paidMethodId != 5){
                            if(!$paidMethod4){
                                $errors[$count] = '佣金支付方式不能为空';
                                continue;
                            }
                            if(!in_array($paidMethod4,ReimburseApply::$_paidMethod)){
                                $errors[$count] = $paidMethod4.'佣金支付方式不存在，请选择'.implode('、',ReimburseApply::$_paidMethod);
                                continue;
                            }
                        }else{
                            $paidMethod4 = $paidMethod;
                        }
                    }
                }else{
                    if(!$orderAtten){
                        $errors[$count] = '多次返款订单联系人不能为空';
                        continue;
                    }
                    if(!is_numeric($paidAmount2)){
                        $errors[$count] = '第一次付款金额格式出错';
                        continue;
                    }
                    if(!is_numeric($paidAmount3)){
                        $errors[$count] = '第二次付款金额格式出错';
                        continue;
                    }
                }
                if($orderNo){
                    if(in_array($orderNo,$orderNoArr)){
                        $errors[$count] = '订单号重复';
                        continue;
                    }else{
                        $orderNoArr[$count] = $orderNo;
                    }
                    $raCount = ReimburseApply::query()->where('order_no',$orderNo)->count();
                    if($raCount > 0){
                        $errors[$count] = $orderNo.'系统已存在';
                        continue;
                    }
                }
                $paidMethodId4 = $paidTypeId == 3 && $paidMethod4 ? array_search($paidMethod4,ReimburseApply::$_paidMethod) : null;
                $rTypeId = $feeTypeId == 1 && $rType ? array_search($rType,ReimburseApply::$_rType) : null;
                $apply = ['fee_type' => $feeTypeId,
                          'r_type' => $rTypeId,
                          'paid_type' => $paidTypeId,
                          'order_no' => $orderNo,
                          'country_id' => $countryId,
                          'seller_sku' => $sellerSku,
                          'asin' => $asin,
                          'seller_id' => $pas->seller_id ?? $userId,
                          'psku_id' => $pas->psku_id ?? null,
                          'paid_method' => $paidMethodId,
                          'paid_account_no' => $paidAccountNo,
                          'currency_code' => $currency,
                          'order_atten' => $orderAtten,
                          'remarks' => $remarks,
                          'operator_id' => $userId,
                          'apply_status' => $paidMethodId != 5 ? 1 : 2,
                          'audit_status' => $paidMethodId != 5 ? 1 : 2,
                          'paid_status' => $paidMethod != 5 ? 1 : 3,
                ];
                $rd = [];
                $rd['fee_type'] = $feeTypeId;
                $rd['r_type'] = $rTypeId;
                $rd['order_no'] = $orderNo;
                $rd['country_id'] = $countryId;
                $rd['seller_sku'] = $sellerSku;
                $rd['seller_id'] = $apply['seller_id'];
                $rd['psku_id'] = $apply['psku_id'];
                $rd['paid_method'] = $paidMethodId;
                $rd['paid_account_no'] = $paidAccountNo;
                $rd['currency_code'] = $currency;
                $rd['remarks'] = $remarks;
                $rd['rate_odds'] = $paidMethodId != 5 ? null : ExchangeRate::getRateOddsToRMB($currency,strtotime($date));
                $rd['paid_status'] = $paidMethodId != 5 ? 1 : 3;
                $rd['paid_date'] = $paidMethodId != 5 ? null : $date;
                $rd['report_date'] = $paidMethodId != 5 ? null : date('Y-m-01',strtotime($date));
                $paid = [];
                if($paidTypeId == 1){
                    $paid[1]['paid_amount'] = $paidAmount1;
                }elseif($paidTypeId == 2){
                    $paid[2]['paid_amount'] = $paidAmount2;
                    $paid[3]['paid_amount'] = $paidAmount3;
                }else{
                    $rd['paid_date_4'] = $paidMethodId != 5 ? null : $date;
                    $paid[1]['paid_amount'] = $paidAmount1;
                    $paid[4]['paid_amount'] = $paidAmount4;
                    $paid[4]['paid_method'] = $paidMethodId != 5 ? $paidMethodId4 : 5;
                    $paid[4]['paid_account_no'] = $paidAccountNo4;
                    $paid[4]['currency_code'] = $currency4;
                }
                $paidItem = [];
                foreach ($paid as $k => $item){
                    $rd['paid_amount_'.$k] = $item['paid_amount'];
                    $paidItem[$k]['order_by'] = $k;
                    $paidItem[$k]['paid_amount'] = $item['paid_amount'];
                    $paidItem[$k]['apply_status'] = $paidMethodId != 5 ? 1 : 2;
                    $paidItem[$k]['audit_status'] = $paidMethodId != 5 ? 1 : 2;
                    $paidItem[$k]['paid_status'] = $paidMethodId != 5 ? 1 : 2;
                    $paidItem[$k]['paid_date'] = $paidMethodId != 5 ? null : $date;
                    if($k != 4){
                        $paidItem[$k]['paid_method'] = $paidMethodId;
                        $paidItem[$k]['paid_account_no'] = $paidAccountNo;
                        $paidItem[$k]['currency_code'] = $currency;
                    }else{
                        $rd['rate_odds_4'] = $paidMethodId != 5 ? null : ExchangeRate::getRateOddsToRMB($item['currency_code'],strtotime($date));
                        $rd['paid_method_4'] = $item['paid_method'];
                        $rd['paid_account_no_4'] = $item['paid_account_no'];
                        $rd['currency_code_4'] = $item['currency_code'];
                        $paidItem[$k]['paid_method'] = $item['paid_method'];
                        $paidItem[$k]['paid_account_no'] = $item['paid_account_no'];
                        $paidItem[$k]['currency_code'] = $item['currency_code'];
                    }
                }
                $items[] = ['apply' => $apply,'paid' => $paidItem,'rd' => $rd];
            }
            if(empty($errors)){
                if(empty($items)){
                    return ['flag' => false,'data' => '文件无数据项！'];
                }else{
                    DB::beginTransaction();
                    $flag = false;
                    foreach($items as $item){
                        $apply = $item['apply'];
                        $paid = $item['paid'];
                        $rd = $item['rd'];
                        $apply['reimburse_no'] = SerialNo::getRaNo();
                        $ra = ReimburseApply::query()->create($apply);
                        $ra->paid()->createMany($paid);
                        $ra->detail()->create($rd);
                        if(!$ra){
                            $flag = FALSE;
                            break;
                        }
                        $flag = TRUE;
                    }
                    if($flag){
                        DB::commit();
                        return ['flag' => true,'data' => $file->getClientOriginalName()];
                    }else{
                        DB::rollBack();
                        return ['flag' => false,'data' => '添加失败！'];
                    }
                }
            }else{
                return RExcel::importResult($errors);
            }
        }catch(\Exception $e){
            report($e);
            return ['flag' => false,'data' => '系统异常！'];
        }
    }

    private function _importInit(UploadedFile $file,$param){
        try{
            $data = Excel::toArray(new ApplyImporter(),$file);
            $data = $data[0];
            if(!$data)return ['flag' => false,'data' => '请上传包含有数据的文件！'];
            $errors = [];
            $count = 1;
            $userId = Admin::user()->id;
            $orderNoArr = [];
            $items = [];
            $date = date('Y-m-d H:i:s',strtotime($param['year']));
            foreach($data as $row){
                $count++;
                $_country = $row[0] ? trim($row[0]) : '';
                $paidType = $row[1] ? trim($row[1]) : '';
                $feeType = $row[2] ? trim($row[2]) : '';
                $rType = $row[3] ? trim($row[3]) : '';
                $orderNo = $row[4] ? trim($row[4]) : '';
                $orderAtten = $row[5] ? trim($row[5]) : '';
                $sellerSku = $row[6] ? trim($row[6]) : '';
                $paidMethod = $row[7] ? mergeSpaces(trim($row[7])) : '';
                $paidAccountNo = $row[8] ? trim($row[8]) : '';
                $_currency = $row[9] ? trim($row[9]) : '';
                $paidAmount1 = $row[10] ? trim($row[10]) : '';
                $paidAmount2 = $row[11] ? trim($row[11]) : '';
                $paidAmount3 = $row[12] ? trim($row[12]) : '';
                $paidAmount4 = $row[13] ? trim($row[13]) : '';
                $paidMethod4 = $row[14] ? mergeSpaces(trim($row[14])) : '';
                $paidAccountNo4 = $row[15] ? trim($row[15]) : '';
                $_currency4 = $row[16] ? trim($row[16]) : '';
                $asin = $row[17] ? trim($row[17]) : '';
                $remarks = $row[18] ? trim($row[18]) : '';
                if(!$_country || !$paidType || !$feeType || !$paidMethod || !$paidAccountNo || !$_currency){
                    $errors[$count] = '必填项为空';
                    continue;
                }
                $country = strtoupper($_country);
                $countryId = SysCountry::getIdByCountry($country);
                if(!$countryId){
                    $errors[$count] = $_country.'系统不存在';
                    continue;
                }
                $areaId = SysICountry::getAreaIdByCountryId($countryId);
                if(!$areaId){
                    $errors[$count] = $_country.'系统未匹配到站点';
                    continue;
                }
                if(!in_array($paidType,ReimburseApply::$_paidType)){
                    $errors[$count] = $paidType.'返款类型不存在，请选择'.implode('、',ReimburseApply::$_paidType);
                    continue;
                }
                $paidTypeId = array_search($paidType,ReimburseApply::$_paidType);
                if(!in_array($feeType,ReimburseApply::$_feeType)){
                    $errors[$count] = $feeType.'费用类型不存在，请选择'.implode('、',ReimburseApply::$_feeType);
                    continue;
                }
                $feeTypeId = array_search($feeType,ReimburseApply::$_feeType);
                if($feeTypeId == 1 || $feeTypeId == 2){
                    if($feeTypeId == 1){
                        if(!$rType){
                            $errors[$count] = '刷单类型不能为空！';
                            continue;
                        }
                        if(!in_array($rType,ReimburseApply::$_rType)){
                            $errors[$count] = $_currency.'刷单类型不存在，请选择'.implode('、',ReimburseApply::$_rType);
                            continue;
                        }
                    }
                    if(!$sellerSku){
                        $errors[$count] = '亚马逊SKU不能为空！';
                        continue;
                    }
                    $pas = ProductAmazonSku::query()
                        ->where('seller_sku',$sellerSku)
                        ->where('country_id',$countryId)
                        ->first();
                    if(!$pas){
                        $errors[$count] = '系统不存在该国家的亚马逊SKU：'.$sellerSku;
                        continue;
                    }
                }elseif($feeTypeId == 4){
                    if(!$asin){
                        $errors[$count] = 'ASIN不能为空！';
                        continue;
                    }
                    $pas = ProductAmazonSku::query()
                        ->where('country_id',$countryId)
                        ->where('asin',$asin)
                        ->first();
                    if(!$pas){
                        $errors[$count] = '系统未匹配到该ASIN：'.$asin;
                        continue;
                    }
                }else{
                    if($sellerSku){
                        $pas = ProductAmazonSku::query()
                            ->where('seller_sku',$sellerSku)
                            ->where('country_id',$countryId)
                            ->first();
                        if(!$pas){
                            $errors[$count] = '系统不存在该国家的亚马逊SKU：'.$sellerSku;
                            continue;
                        }
                    }
                }
                if(!in_array($paidMethod,ReimburseApply::$_paidMethod)){
                    $errors[$count] = $paidMethod.'支付方式不存在，请选择'.implode('、',ReimburseApply::$_paidMethod);
                    continue;
                }
                $paidMethodId = array_search($paidMethod,ReimburseApply::$_paidMethod);
                $currency = strtoupper($_currency);
                if(!Currency::existByCode($currency)){
                    $errors[$count] = $_currency.'币种不存在';
                    continue;
                }
                $currency4 = null;
                if($paidTypeId == 1 || $paidTypeId == 3){
                    if(!is_numeric($paidAmount1)){
                        $errors[$count] = '一次性付款金额格式出错';
                        continue;
                    }
                    if($paidTypeId == 3){
                        if(!is_numeric($paidAmount4)){
                            $errors[$count] = '佣金金额格式出错';
                            continue;
                        }
                        if(!$_currency4){
                            $errors[$count] = '佣金币种不能为空';
                            continue;
                        }
                        $currency4 = strtoupper($_currency4);
                        if(!Currency::existByCode($currency4)){
                            $errors[$count] = $_currency4.'佣金币种不存在';
                            continue;
                        }
                        if($paidMethodId != 5){
                            if(!$paidMethod4){
                                $errors[$count] = '佣金支付方式不能为空';
                                continue;
                            }
                            if(!in_array($paidMethod4,ReimburseApply::$_paidMethod)){
                                $errors[$count] = $paidMethod4.'佣金支付方式不存在，请选择'.implode('、',ReimburseApply::$_paidMethod);
                                continue;
                            }
                        }else{
                            $paidMethod4 = $paidMethod;
                        }
                    }
                }else{
                    if(!$orderAtten){
                        $errors[$count] = '多次返款订单联系人不能为空';
                        continue;
                    }
                    if(!is_numeric($paidAmount2)){
                        $errors[$count] = '第一次付款金额格式出错';
                        continue;
                    }
                    if(!is_numeric($paidAmount3)){
                        $errors[$count] = '第二次付款金额格式出错';
                        continue;
                    }
                }
                if($orderNo){
                    if(in_array($orderNo,$orderNoArr)){
                        $errors[$count] = '订单号重复';
                        continue;
                    }else{
                        $orderNoArr[$count] = $orderNo;
                    }
                    $raCount = ReimburseApply::query()->where('order_no',$orderNo)->count();
                    if($raCount > 0){
                        $errors[$count] = $orderNo.'系统已存在';
                        continue;
                    }
                }
                $paidMethodId4 = $paidTypeId == 3 && $paidMethod4 ? array_search($paidMethod4,ReimburseApply::$_paidMethod) : null;
                $rTypeId = $feeTypeId == 1 && $rType ? array_search($rType,ReimburseApply::$_rType) : null;
                $apply = ['fee_type' => $feeTypeId,
                          'r_type' => $rTypeId,
                          'paid_type' => $paidTypeId,
                          'order_no' => $orderNo,
                          'country_id' => $countryId,
                          'seller_sku' => $sellerSku,
                          'asin' => $asin,
                          'seller_id' => $pas->seller_id ?? $userId,
                          'psku_id' => $pas->psku_id ?? null,
                          'paid_method' => $paidMethodId,
                          'paid_account_no' => $paidAccountNo,
                          'currency_code' => $currency,
                          'order_atten' => $orderAtten,
                          'remarks' => $remarks,
                          'operator_id' => $userId,
                          'apply_status' => 2,
                          'audit_status' => 2,
                          'paid_status' => 3,
                ];
                $rd = [];
                $rd['fee_type'] = $feeTypeId;
                $rd['r_type'] = $rTypeId;
                $rd['order_no'] = $orderNo;
                $rd['country_id'] = $countryId;
                $rd['seller_sku'] = $sellerSku;
                $rd['seller_id'] = $apply['seller_id'];
                $rd['psku_id'] = $apply['psku_id'];
                $rd['paid_method'] = $paidMethodId;
                $rd['paid_account_no'] = $paidAccountNo;
                $rd['currency_code'] = $currency;
                $rd['remarks'] = $remarks;
                $rd['rate_odds'] = ExchangeRate::getRateOddsToRMB($currency,strtotime($date));
                $rd['paid_status'] = 3;
                $rd['paid_date'] = $date;
                $rd['report_date'] = date('Y-m-01',strtotime($date));
                $paid = [];
                if($paidTypeId == 1){
                    $paid[1]['paid_amount'] = $paidAmount1;
                }elseif($paidTypeId == 2){
                    $paid[2]['paid_amount'] = $paidAmount2;
                    $paid[3]['paid_amount'] = $paidAmount3;
                }else{
                    $rd['paid_date_4'] = $date;
                    $paid[1]['paid_amount'] = $paidAmount1;
                    $paid[4]['paid_amount'] = $paidAmount4;
                    $paid[4]['paid_method'] = $paidMethodId != 5 ? $paidMethodId4 : 5;
                    $paid[4]['paid_account_no'] = $paidAccountNo4;
                    $paid[4]['currency_code'] = $currency4;
                }
                $paidItem = [];
                foreach ($paid as $k => $item){
                    $rd['paid_amount_'.$k] = $item['paid_amount'];
                    $paidItem[$k]['order_by'] = $k;
                    $paidItem[$k]['paid_amount'] = $item['paid_amount'];
                    $paidItem[$k]['apply_status'] = 2;
                    $paidItem[$k]['audit_status'] = 2;
                    $paidItem[$k]['paid_status'] = 2;
                    $paidItem[$k]['paid_date'] = $date;
                    if($k != 4){
                        $paidItem[$k]['paid_method'] = $paidMethodId;
                        $paidItem[$k]['paid_account_no'] = $paidAccountNo;
                        $paidItem[$k]['currency_code'] = $currency;
                    }else{
                        $rd['rate_odds_4'] = ExchangeRate::getRateOddsToRMB($item['currency_code'],strtotime($date));
                        $rd['paid_method_4'] = $item['paid_method'];
                        $rd['paid_account_no_4'] = $item['paid_account_no'];
                        $rd['currency_code_4'] = $item['currency_code'];
                        $paidItem[$k]['paid_method'] = $item['paid_method'];
                        $paidItem[$k]['paid_account_no'] = $item['paid_account_no'];
                        $paidItem[$k]['currency_code'] = $item['currency_code'];
                    }
                }
                $items[] = ['apply' => $apply,'paid' => $paidItem,'rd' => $rd];
            }
            if(empty($errors)){
                if(empty($items)){
                    return ['flag' => false,'data' => '文件无数据项！'];
                }else{
                    DB::beginTransaction();
                    $flag = false;
                    foreach($items as $item){
                        $apply = $item['apply'];
                        $paid = $item['paid'];
                        $rd = $item['rd'];
                        $apply['reimburse_no'] = SerialNo::getRaNo();
                        $ra = ReimburseApply::query()->create($apply);
                        $ra->paid()->createMany($paid);
                        $ra->detail()->create($rd);
                        if(!$ra){
                            $flag = FALSE;
                            break;
                        }
                        $flag = TRUE;
                    }
                    if($flag){
                        DB::commit();
                        return ['flag' => true,'data' => $file->getClientOriginalName()];
                    }else{
                        DB::rollBack();
                        return ['flag' => false,'data' => '添加失败！'];
                    }
                }
            }else{
                return RExcel::importResult($errors);
            }
        }catch(\Exception $e){
            report($e);
            return ['flag' => false,'data' => '系统异常！'];
        }
    }

    public function template(Request $request){
        $temp = $request->get('temp');
        $suffix = $request->get('suffix','xlsx');
        return Storage::disk('admin')->download("template/{$temp}-template.{$suffix}");
    }
}
